define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: location.href,
                    add_url: 'xilujob/user_recommend_job/search_job/ids/'+Fast.api.query('ids'),
                    //edit_url: 'xilujob/user_recommend_job/edit',
                    del_url: 'xilujob/user_recommend_job/del',
                    table: 'xilujob_user_recommend_job',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'job_id', title: __('Job_id')},
                        {field: 'company_id', title: __('Company_id')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        search_job: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: location.href,
                    add_url: 'xilujob/job/add',
                    table: 'xilujob_job',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport:false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'enterpriseinfo.company_name', title: '公司名', operate:'like'},
                        {field: 'job_name', title: '职位名称', operate:'like'},
                        {field: 'type', title: __('类型'), searchList: {1:'全职',2:'兼职'}, formatter: Table.api.formatter.text},
                        {field: 'job_category_id', title: '职位分类', searchList: Config.categorySearch, formatter: function(val,row){return row.job_category.name_path}},
                        {field: 'num', title: '招聘人数', operate:false},
                        {field: 'refreshed_at', title: '发布时间', operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name: 'recommend',
                                    title: '推荐',
                                    text: '推荐',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    click: function (config,row,btn) {
                                        var $this = $(this);
                                        Fast.api.ajax({
                                            url: 'xilujob/user_recommend_job/add',
                                            data:{user_id: Fast.api.query('ids'), job_id: row.id}
                                        },function() {
                                            parent.$(".btn-refresh").trigger("click");
                                            $this.remove();
                                        });
                                    },
                                    visible: function (row) {
                                        return row.user_id===null;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('#toolbar .btn-recommend').on('click', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                console.log(ids);
                Fast.api.ajax({
                    url: 'xilujob/user_recommend_job/add',
                    data:{user_id: Fast.api.query('ids'), job_id: ids.join(',')}
                },function() {
                    parent.$(".btn-refresh").trigger("click");
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});